import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { NavController, LoadingController, ToastController } from 'ionic-angular';

import { TaskService } from '../../providers/task';

@Component({
	selector: 'page-register',
	templateUrl: 'register.html',
	providers: [TaskService]
})
export class RegisterPage {

	constructor(
		private navCtrl: NavController,
		private task: TaskService,
		private loadingCtrl: LoadingController,
		private toastCtrl: ToastController,
		private storage: Storage
	) { }

	oId: string;
	profile: any = {
		name: '',
		birthday: '',
		telphone: '',
		nick_name: ''
	};

	ionViewDidLoad() {
		this.storage.get('openid').then(v => this.oId = v);
	};

	// 提交数据
	save() {
		let loader = this.loadingCtrl.create({ content: '处理中...' });
		loader.present();
		this.task.fillProfile(this.profile, this.oId).then(result => {
			loader.dismiss();
			if (result === '10000') {
				let toast = this.toastCtrl.create({
					message: '修改成功',
					duration: 1000,
					cssClass: 'action-success',
					position: 'bottom'
				});
				toast.onDidDismiss(() => {
					this.storage.remove('fan');
					this.navCtrl.pop();
				});
				toast.present();
			} else {
				let toast = this.toastCtrl.create({
					message: '操作失败',
					duration: 2000,
					cssClass: 'action-error',
					position: 'bottom'
				});
				toast.present();
			}
		});
	};
}
